package com.sulj.domain;

import com.sulj.enums.FruitType;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 水果类
 *
 * @author slj
 * @date 2025-03-29
 */
public class Fruit implements Serializable {

    /**
     * 名称
     */
    private String name;


    /**
     * 单价
     */
    private BigDecimal unitPrice;

    /**
     * 类型
     */
    private FruitType type;

    /**
     * 折扣值，如0.8代表8折
     */
    private BigDecimal discountValue;

    public Fruit(FruitType type, String name, BigDecimal unitPrice) {
        this.name = Objects.requireNonNull(name, "名称不能为空");
        this.unitPrice = Objects.requireNonNull(unitPrice, "水果单价不能为空");
        this.type = Objects.requireNonNull(type, "水果类型不能为空");
        if (unitPrice.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("水果单价不能为负数‌");
        }
    }

    public Fruit(FruitType type, String name, BigDecimal unitPrice, BigDecimal discountValue) {
        this(type, name, unitPrice);
        this.discountValue = Objects.requireNonNull(discountValue, "折扣值不能为空");
        if (discountValue.compareTo(BigDecimal.ZERO) < 0 || discountValue.compareTo(BigDecimal.ONE) > 0) {
            throw new IllegalArgumentException("折扣数值范围0至1之间");
        }
    }

    /**
     * 计算总价
     *
     * @param quantity 数量
     * @return 总价
     */
    public BigDecimal calculateTotal(int quantity) {
        BigDecimal total = unitPrice.multiply(BigDecimal.valueOf(quantity));
        if (discountValue != null) {
            total = total.multiply(discountValue);
        }
        return total;
    }

    public String getName() {
        return name;
    }


}
